package cn.tedu.cart.service.impl;

import cn.tdu.common.feign.StockClient;
import cn.tedu.cart.mapper.CartMapper;
import cn.tedu.cart.service.CartService;
import cn.tedu.pojo.param.CartAddParam;
import cn.tedu.pojo.po.CartPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 购物车业务的实现
 */
@Service
@Slf4j
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private StockClient stockClient;

    /**
     * 添加购物车
     * @param cartAddParam
     * @return
     */
    @Override
    public Boolean addCart(CartAddParam cartAddParam) {
        log.info("添加购物车操作，传入的参数：cartAddParam={}", cartAddParam);

        // 参数校验
        if (cartAddParam == null) {
            log.warn("购物车参数不能为空");
            return false;
        }

        if (cartAddParam.getProductCode() == null || cartAddParam.getProductCode().trim().isEmpty()) {
            log.warn("商品编码不能为空");
            return false;
        }

        if (cartAddParam.getUserId() == null || cartAddParam.getUserId().trim().isEmpty()) {
            log.warn("用户ID不能为空");
            return false;
        }

        if (cartAddParam.getCount() == null || cartAddParam.getCount() <= 0) {
            log.warn("商品数量必须大于0");
            return false;
        }

        try {
            // 先检查库存数量
            Integer stockCount = stockClient.checkStock(cartAddParam.getProductCode());
            log.info("查询到的库存数量为：stockCount={}", stockCount);
            if (stockCount == null || stockCount < cartAddParam.getCount()) {
                log.warn("库存不足，库存的数量为：{}", stockCount);
                return false;
            }

            // 1.将入参param 写入数据 转成po(po 和持久层强绑定的)
            // 2.调用持久层
            CartPO cartPO = new CartPO();
            BeanUtils.copyProperties(cartAddParam, cartPO);
            int result = cartMapper.insert(cartPO);
            log.info("添加购物车操作，返回结果：result={}", result);
            if (result < 1) {
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("添加购物车时发生异常：", e);
            return false;
        }
    }

    /**
     * 删除购物车
     * @param userId
     * @param productCode
     * @return
     */
    @Override
    public Boolean deleteCart(String userId, String productCode) {
        // 参数校验
        if (userId == null || userId.trim().isEmpty()) {
            log.warn("用户ID不能为空");
            return false;
        }

        if (productCode == null || productCode.trim().isEmpty()) {
            log.warn("商品编码不能为空");
            return false;
        }

        // delete from cart_tbl where product_code=#{productCode} and user_id=#{userId}
        // 需要为delete update select 拼接where条件，使用QueryWrapper工具完成
        log.info("删除购物车操作，传入的参数：userId={},productCode={}", userId, productCode);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_code", productCode);
        queryWrapper.eq("user_id", userId);
        int deleteRows = cartMapper.delete(queryWrapper);
        log.info("删除购物车操作，返回结果：deleteRows={}", deleteRows);
        if (deleteRows < 1) {
            log.error("删除购物车操作失败");
            return false;
        }
        log.info("删除购物车操作成功");
        return true;
    }
}
